package com.arpa.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.wms.domain.dto.PrepackagedItemDTO;
import com.arpa.wms.domain.entity.Prepackaged;
import com.arpa.wms.domain.entity.PrepackagedItem;
import com.arpa.wms.domain.vo.InventoryWithDistributionVO;
import com.arpa.wms.domain.vo.PrepackagedItemVO;
import com.arpa.wms.domain.vo.PrepackagedItemWithDistributionVO;
import com.arpa.wms.mapper.PrepackagedItemMapper;
import com.arpa.wms.service.IInventoryService;
import com.arpa.wms.service.IPrepackagedItemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 预打包单 服务实现类
 * </p>
 *
 * @author leo
 * @since 2021-07-02
 */
@Service
public class PrepackagedItemServiceImpl extends ServiceImpl<PrepackagedItemMapper, PrepackagedItem> implements IPrepackagedItemService {

    @Autowired
    private IInventoryService inventoryService;
    /**
     * 获取 预打包明细可用存库
     *
     * @param prepackagedItemDTO
     * @return
     */
    @Override
    public Result queryInventory(PrepackagedItemDTO prepackagedItemDTO) {
        PrepackagedItemVO prepackagedItemVO = new PrepackagedItemVO();
        List<InventoryWithDistributionVO> inventoryWithDistributionVOS = inventoryService.queryCanDistributeInventory(prepackagedItemDTO.getWarehouseCode(),prepackagedItemDTO.getShipmentCode(), prepackagedItemDTO.getGoodsCode());
        BigDecimal availableStockQuantity = inventoryWithDistributionVOS.stream().map(InventoryWithDistributionVO::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
        prepackagedItemVO.setAvailableStockQuantity(availableStockQuantity);
        return Result.ok().data(prepackagedItemVO);

    }

    /**
     * 调整分配数量，正数表示增加，负数表示减少
     * @param prepackagedItemDTO
     * @return
     */
    @Override
    public int adjustDistributionQuantity(PrepackagedItemDTO prepackagedItemDTO) {
        prepackagedItemDTO.setGroupCode(UserUtil.getBranchCode());
        return this.baseMapper.adjustDistributionQuantity(prepackagedItemDTO);
    }

    /***
     * @description
     * 根据预打包单号，更新预打包明细拣货数量
     * @author rfwang
     * @date 2021年7月7日
     * @param prepackagedCode:
     **/
    @Override
    @Transactional
    public int updatePickingQuantity(String prepackagedCode){
        return this.baseMapper.updatePickingQuantity(prepackagedCode);
    }

    /**
     * 根据预打包表code获取明细关联列表
     * @param prepackagedCode
     * @return
     */
    @Override
    public List<PrepackagedItemWithDistributionVO> queryItemList(String prepackagedCode){
        List<PrepackagedItem> prepackagedItemList = this.list(new LambdaQueryWrapper<PrepackagedItem>()
                .eq(PrepackagedItem::getPrepackagedCode, prepackagedCode));
        return prepackagedItemList.stream().map(info -> {
            PrepackagedItemWithDistributionVO itemWithDistributionVO = new PrepackagedItemWithDistributionVO();
            BeanUtil.copyProperties(info, itemWithDistributionVO);
            return itemWithDistributionVO;
        }).collect(Collectors.toList());
    }
}
